<extend name="Layout:list" />
<blcok name="title"><title>角色授权</title></blcok>
<block name="subheader">
    <link rel="stylesheet" type="text/css" href="{:asset('css/admin/jquery.treetable.css')}" />
    <link rel="stylesheet" type="text/css" href="{:asset('css/admin/jquery.treetable.theme.default.css')}" />
    <link href="<?php echo asset('admin/css/inbox.css') ?>" rel="stylesheet" type="text/css"/>
</block>
<block name="subnav">
    <a class="btn blue" href="__MODULE__/Role/add">添加角色</a>&nbsp;&nbsp;<a class="btn blue" href="__MODULE__/Role/index">角色管理</a>&nbsp;&nbsp;<a class="btn red" href="javascript:void(0);">权限分配</a>
</block>
<block name="main">

    <form name="myform" id="myform" action="__MODULE__/Role/limits" method="post">
        <input name="role_id" type="hidden" value="<?php echo $role_id; ?>" />
            <div class="portlet box grey">
                <div class="portlet-title">
                    <div class="caption"><i class="icon-comments"></i>权限分配</div>
                    <div class="actions">
                        <a href="javascript:void(0)" onclick="select_all();" class="btn green">全选</a>
                        <a href="javascript:void(0)" onclick="jQuery('#treetable').treetable('expandAll'); return false;" class="expandAll btn green">展开</a>
                        <a href="javascript:void(0)" onclick="jQuery('#treetable').treetable('collapseAll'); return false;" class="collapseAll  btn yellow">收缩</a>
                    </div>
                </div>
                <div class="portlet-body">
                    <div class="row-fluid">
                        <div class="span2">
                            <div class="site-list inbox">
                                <ul class="inbox-nav margin-bottom-10">
                                    <li class="compose-btn">
                                        <a href="javascript:;" data-title="Compose" class="btn green">
                                            <i class="icon-edit"></i> 站点列表
                                        </a>
                                    </li>
                                    <?php foreach ($sites as $key => $site) { ?>
                                    <li class="inbox <?php echo ($site['id'] == $current_site['id']) ? 'active' : ''; ?>">
                                        <a href="__SELF__/siteid/<?php echo $site['id']; ?>" class="btn"><?php echo $site['name']; ?></a>
                                    </li>
                                    <?php } ?>
                                </ul>
                            </div>
                        </div>
                        <div class="span10" style="margin-left: 20px;">
                            <table id="treetable" class="form-inline" style="margin-top: 0;">
                                <tbody>
                                    <input type="hidden" name="site" value="<?php echo $current_site['id']; ?>">
                                    <?php foreach ($menus as $key => $menu) { ?>
                                    <tr data-tt-id='<?php echo $menu['id']; ?>' <?php if($menu['pid']) echo "data-tt-parent-id='".$menu['pid']."'"; ?>>
                                        <td>
                                            <span <?php echo ($menu['_child'] ? "class='folder'" : "class='file' style='margin-left: 19px;'" ); ?>>
                                                <input type="checkbox" name="menuid[]" value="<?php echo $menu['id'] ?>" <?php if(isset($authorized[$menu['id']])) echo "checked"; ?> /><?php echo $menu['title']; ?>
                                                <?php if($menu['request_method']) { ?>
                                                <label><input type="radio" name="menu[<?php echo $menu['id']; ?>]" value="0" <?php if(isset($authorized[$menu['id']]) && $authorized[$menu['id']]['request_method'] == 0 ) echo "checked"; ?> />ALL</label>
                                                <label><input type="radio" name="menu[<?php echo $menu['id']; ?>]" value="1" <?php if(isset($authorized[$menu['id']]) && $authorized[$menu['id']]['request_method'] == '1' ) echo "checked"; ?> />GET</label>
                                                <label><input type="radio" name="menu[<?php echo $menu['id']; ?>]" value="2" <?php if(isset($authorized[$menu['id']]) && $authorized[$menu['id']]['request_method'] == '2' ) echo "checked"; ?> />POST</label>
                                                <?php } ?>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                            <div class="mt-20 clearfix">
                                <input name="dosubmit" type="submit" class="btn red" value="提交">&nbsp;&nbsp;<input type="reset" value="取消" class="btn">
                            </div>
                        </div>
                    </div>
                </div>
        </div>
    </form>
</block>
<block name="footer">
    <script src="{:asset('js/admin/jquery.treetable.js')}"></script>
    <script type="text/javascript">
        function select_all() {
            $('#treetable input[type="checkbox"]').prop("checked", true);
            $("#treetable .expandAll").click();
        }

        function change_checkbox_parent(pid) {
            var dom_tr = $("#treetable tr[data-tt-id='" + pid +"']");
            if (dom_tr.length > 0) {
                $(dom_tr).each(function(index, dom){
                    var input_dom = $(dom).children("td").children("span").children("input");
                    $(input_dom).prop("checked", "checked");
                    change_checkbox_parent($(dom).data('tt-parent-id'));
                })
            }
        }

        function change_checkbox_child(id,status) {
            var dom_tr = $("#treetable tr[data-tt-parent-id='" + id +"']");
            if (dom_tr.length > 0) {
                $(dom_tr).each(function(index, dom){
                    var input_dom = $(dom).children("td").children("span").children("input");
                    $(input_dom).prop("checked", status);
                    change_checkbox_child($(dom).data("tt-id"), status);
                })
            }
        }
        $("#treetable").treetable({
            expandable: true,
            onInitialized:function () {
                $("#treetable").css('display','inline-table');
                $(".expandAll").trigger('click');
            }
        });

        $("#treetable tbody td").on("change", "input[type='checkbox']", function() {
            if ($(this).prop("checked")) {
                var parent_id = $(this).closest("tr").data("tt-parent-id");
                if (parent_id) {
                    change_checkbox_parent(parent_id);
                }
                var id = $(this).closest("tr").data("tt-id");
                if (id) {
                    change_checkbox_child(id,true);
                };
            } else {
                var id = $(this).closest("tr").data("tt-id");
                if (id) {
                    change_checkbox_child(id,false);
                }
            }
        });
    </script>
</block>